<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动态创建表格</title>
    <style>
      table {
        width: 500px;
        margin: 100px auto;
        text-align: center;
        font-size: 14px;
        border-collapse: collapse;
      }

      thead > tr {
        height: 30px;
        background-color: #caccca;
      }

      tbody > tr {
        height: 30px;
      }
    </style>
  </head>
  <body>
    <table border="1">
      <thead>
        <tr>
          <th>姓名</th>
          <th>科目</th>
          <th>成绩</th>
          <th>操作</th>
        </tr>
      </thead>

      <tbody></tbody>
    </table>

    <script>
      // 表格数据
      var datas = [
        {
          name: '魏璎珞',
          subject: 'JavaScript',
          score: 100
        },
        {
          name: '弘历',
          subject: 'JavaScript',
          score: 90
        },
        {
          name: '傅恒',
          subject: 'JavaScript',
          score: 99
        },
        {
          name: '明玉',
          subject: 'JavaScript',
          score: 80
        }
      ];

      var tbody = document.querySelector('tbody');

      // 1、多少人对应多少行
      for (var i = 0; i < datas.length; i++) {
        var tr = document.createElement('tr');
        tbody.appendChild(tr);

        // 2、单元格数量对应每个对象的属性个数
        for (var k in datas[i]) {
          var td = document.createElement('td');
          td.innerHTML = datas[i][k];
          tr.appendChild(td);
        }

        // 3、添加删除单元格
        var td = document.createElement('td');
        td.innerHTML = '<a href="javascript:;">删除</a>';
        tr.appendChild(td);
      }

      // 4、删除操作
      var as = document.querySelectorAll('a');
      for (var e = 0; e < as.length; e++) {
        as[e].onclick = function () {
          tbody.removeChild(this.parentNode.parentNode);
        };
      }
    </script>
  </body>
</html>
